import sai.mysql.DriverLoading;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class PatientUpdate extends HttpServlet
{
PreparedStatement prepare,prepare1;
Statement state,state1,state2,state3,state4;
ResultSet result,result1,result2;
String temp;
    @Override
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
	res.setContentType("text/html");
	PrintWriter out = res.getWriter();
	try
	{
	String patientname=req.getParameter("pname");
	String fathername=req.getParameter("fname");
	String dob=req.getParameter("dob");	
	String gender=req.getParameter("gender");		
	String contact=req.getParameter("mobile"); 
        String patientid=req.getParameter("pid");
	out.println(patientid);
        String email=req.getParameter("email");        
	String username=req.getParameter("username");
	String password=req.getParameter("repassword");
	String status=req.getParameter("status");
	String address=req.getParameter("address");       
	String patient="patient";
        
	DriverLoading dl = new DriverLoading(); 
        
        String url1="select `userid` from `patient` where `patient_id`='"+patientid+"'";
        
        state=dl.getStatement();
        result=state.executeQuery(url1);
        
        boolean b=result.next();
        if(b==true)
        {
            temp=result.getString(1);
        }
        out.println(temp);
        
        String url2="delete from `patient` where `patient_id`='"+patientid+"'";
        state1=dl.getStatement();
        int z=state1.executeUpdate(url2);
        
        String url3="delete from `logindetails` where `userid`='"+temp+"'";  
         state2=dl.getStatement();
        int x=state2.executeUpdate(url3);
        /*String url="update `logindetails` set `USERID`='"+username+"',"
                + "`PASSWORD`='"+password+"',"
                +"`EMAIL`=+'"+email+"',"
                + "`TYPE`='"+patient+"' where `TYPE`='"+patient+"'";
       state3=dl.getStatement();
       int p = state3.executeUpdate(url);*/
        
       
        String url="insert into `logindetails` values(?,?,?,?)";
        prepare1=dl.getPreparedStatement(url);
        prepare1.setString(1,username);
        prepare1.setString(2,password);
        prepare1.setString(3,email);
        prepare1.setString(4,patient);
        prepare1.executeUpdate();
        
      /*  String url1="update `patient` set `PATIENT_ID`='"+patientid+"'"
                + ",`USERID`='"+username+"'"
                + ",`PATIENT_NAME`='"+patientname+"'"
                + ",`FATHER_NAME`='"+fathername+"'"
                + ",`GENDER`='"+gender+"'"
                + ",`DOB`='"+dob+"'"
                + ",`EMAIL`='"+email+"'"
                + ",`MARTIAL_STATUS`='"+status+"'"
                + ",`ADDRESS`='"+address+"'"
                + ",`MOBILE`='"+contact+"'"
                + ",`IMAGE`="+null+" where PATIENT_ID='"+patientid+"'";     
        state4=dl.getStatement();
        int y =state4.executeUpdate(url1);*/
        prepare = dl.getPreparedStatement("insert into `patient` values(?,?,?,?,?,?,?,?,?,?,?)");
        prepare.setString(1,patientid);
        prepare.setString(2,username);
        prepare.setString(3,patientname);
        prepare.setString(4,fathername);
        prepare.setString(5,gender);
        prepare.setString(6,dob);
        prepare.setString(7,email);
        prepare.setString(8,status);
        prepare.setString(9,address);
        prepare.setString(10,contact);
        prepare.setString(11,null);
        prepare.executeUpdate();
	
	}
	catch(Exception e)
	{
	out.println("Exception raised"+e.getMessage());
	}
	
	}
}